package com.bayleaf.core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bayleaf.dao.HSQLConnection;

public abstract class BayleafAbstractDAO {
	protected ResultSet ExcuteQuery(String sql, String... params) {

		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement psmt = null;

		try {
			conn = HSQLConnection.getConnection();
			conn.setAutoCommit(true);
			psmt = conn.prepareStatement(sql);

			for(int i = 0 ; i < params.length ; i++){
				try{
					psmt.setString(i+1, params[i]);
				} catch(SQLException e){
					//파라미터가 더이상 없음
					break;
				}
			}

			if(sql.contains("SELECT") )
				rs = psmt.executeQuery();
			else{
				psmt.execute();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbClose(conn, psmt);
		}

		return rs;
	}

	private void DbClose(Connection conn, PreparedStatement psmt) {
		HSQLConnection.close(psmt);
		HSQLConnection.close(conn);
	}
}
